﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WestWorldRemake;

namespace WestWorldRemake.Messaging
{
    public class Telegram
    {
        private static double SmallestDelay = .25;

        private Telegram() { }

        public Telegram(DateTime time, BaseGameEntity sender, BaseGameEntity receiver, Messages.BaseMessage message, object ExtraInfo)
        {
            DispatchTime = time;
            Sender = sender;
            Receiver = receiver;
            Message = message;
            this.ExtraInfo = ExtraInfo;
        }

        public BaseGameEntity Sender;
        public BaseGameEntity Receiver;
        public Messages.BaseMessage Message;
        public DateTime DispatchTime;
        public object ExtraInfo;

        public override bool Equals(object t)
        {
            return (t.GetType() == this.GetType() && (this == (Telegram)t));
        }
        public static bool operator ==(Telegram t1, Telegram t2)
        {
            return (Math.Abs((t1.DispatchTime.Ticks - t2.DispatchTime.Ticks)/TimeSpan.TicksPerSecond) > SmallestDelay
                && (t1.Sender == t2.Sender)
                && (t1.Receiver == t2.Receiver)
                && (t1.Message == t2.Message));
        }
        public static bool operator !=(Telegram t1, Telegram t2)
        {
            return !(t1 == t2);
        }

        public static bool operator <(Telegram t1, Telegram t2)
        {
            return (t1 == t2) ? false : (t1.DispatchTime < t2.DispatchTime);
        }
        public static bool operator >(Telegram t1, Telegram t2)
        {
            return (t1 == t2) ? false : (t1.DispatchTime > t2.DispatchTime);
        }

        public string MessageDetails(Telegram t)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("time: " + t.DispatchTime);
            sb.Append("  Sender: " + t.Sender);
            sb.Append("  Receiver: " + t.Receiver);
            sb.Append("  Msg: " + t.Message);

            return sb.ToString();
        }
    }
}
